package com.petalsgroup.petals.web.servlet.admin;

import com.petalsgroup.petals.pojo.CommentsDO;
import com.petalsgroup.petals.pojo.UserDO;
import com.petalsgroup.petals.service.admin.AdminService;
import com.petalsgroup.petals.service.admin.CommentAdminService;
import com.petalsgroup.petals.service.admin.UserAdminService;
import com.petalsgroup.petals.service.admin.impl.AdminServiceIpml;
import com.petalsgroup.petals.service.admin.impl.CommentAdminServiceIpml;
import com.petalsgroup.petals.service.admin.impl.UserAdminServiceIpml;
import com.petalsgroup.petals.util.LimitUtil;
import com.petalsgroup.petals.web.BaseServlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/userAdmin/*")
public class UserManagementServlet extends BaseServlet {
    UserAdminService uas = new UserAdminServiceIpml();
    Object pageFlag =null;
    /**
     * 尝试在用户管理模块获取已经登陆的用户
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     * @author wgd
     */
    public void find(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Object admin = req.getSession().getAttribute("loginAdmin");
        writeValue(resp,admin);
    }
    /**
     * 获取用户信息list
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     * @author wgd
     */
    public void userList(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.getSession().setAttribute("pageFlag","userListFlag");

        req.setCharacterEncoding("utf-8");

        LimitUtil userAdminLimit = new LimitUtil();
        req.setAttribute("userAdminLimit", userAdminLimit);
        //List<UserDO> list = as.findAll();
        List<UserDO> list = uas.findAllUserByPages(userAdminLimit);
        req.setAttribute("list",list);
        req.getRequestDispatcher("/WEB-INF/admin_view/userAdmin.jsp").forward(req, resp);
    }

    /**
     * 分页获取获取用户信息list
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     * @author wgd
     */
    public void userAdminPageJump(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        req.setAttribute("barid","userAdmin");
        LimitUtil userAdminLimit = new LimitUtil(Long.valueOf(req.getParameter("total")),
                Integer.valueOf(req.getParameter("num")),
                Integer.valueOf(req.getParameter("currentPage")));
        //List<UserDO> list = as.findAll();
        req.setAttribute("userAdminLimit", userAdminLimit);
        List<UserDO> list = uas.findAllUserByPages(userAdminLimit);
        req.setAttribute("list",list);
        req.getRequestDispatcher("/WEB-INF/admin_view/userAdmin.jsp").forward(req, resp);
    }
    /**
     * 禁言用户
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     * @author wgd
     */
    public void bannedUser(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.获取参数 - uid
        String uid = req.getParameter("uid");
        //2.调用service的方法 - 禁言
        uas.bannedUser(uid);
        pageFlag = req.getSession().getAttribute("pageFlag");
        if ("mixSearchListFlag"==pageFlag){
            req.getRequestDispatcher("/userAdmin/findUserByUsernameNameEmail").forward(req,resp);
        }else if ("userListFlag"==pageFlag){
            req.getRequestDispatcher("/userAdmin/userAdminPageJump").forward(req,resp);
        }else if ("idSearchListFlag"==pageFlag){
            req.getRequestDispatcher("/userAdmin/findUserById").forward(req,resp);
        }else if ("usernameSearchListFlag"==pageFlag){
            req.getRequestDispatcher("/userAdmin/findUserByUsername").forward(req,resp);
        }else if ("emailSearchListFlag"==pageFlag){
            req.getRequestDispatcher("/userAdmin/findUserByEmail").forward(req,resp);
        }else if ("bannedUserListFlag"==pageFlag){
            req.getRequestDispatcher("/userAdmin/findBannedUser").forward(req,resp);
        }
        //req.getRequestDispatcher("/WEB-INF/admin_view/userAdmin.jsp").forward(req, resp);
    }
    /**
     * 解禁用户
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     * @author wgd
     */
    public void resolveUser(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.获取参数 - id
        String uid = req.getParameter("uid");
        //2.调用service的方法 - 解禁
        uas.resolveUser(uid);
        pageFlag = req.getSession().getAttribute("pageFlag");
        if ("mixSearchListFlag"==pageFlag){
            req.getRequestDispatcher("/userAdmin/findUserByUsernameNameEmail").forward(req,resp);
        }else if ("userListFlag"==pageFlag){
            req.getRequestDispatcher("/userAdmin/userAdminPageJump").forward(req,resp);
        }else if ("idSearchListFlag"==pageFlag){
            req.getRequestDispatcher("/userAdmin/findUserById").forward(req,resp);
        }else if ("usernameSearchListFlag"==pageFlag){
            req.getRequestDispatcher("/userAdmin/findUserByUsername").forward(req,resp);
        }else if ("emailSearchListFlag"==pageFlag){
            req.getRequestDispatcher("/userAdmin/findUserByEmail").forward(req,resp);
        }else if ("bannedUserListFlag"==pageFlag){
            req.getRequestDispatcher("/userAdmin/findBannedUser").forward(req,resp);
        }
        //req.getRequestDispatcher("/admin/userAdminPageJump").forward(req,resp);

    }

    /**
     * 注销用户
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     * @author wgd
     */
    public void deleteUser(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.获取参数 - id
        String uid = req.getParameter("uid");
        //2.调用service的方法 - 删除
        uas.deleteUser(uid);
        pageFlag = req.getSession().getAttribute("pageFlag");
        if ("mixSearchListFlag"==pageFlag){
            req.getRequestDispatcher("/userAdmin/findUserByUsernameNameEmail").forward(req,resp);
        }else if ("userListFlag"==pageFlag){
            req.getRequestDispatcher("/userAdmin/userAdminPageJump").forward(req,resp);
        }else if ("idSearchListFlag"==pageFlag){
            req.getRequestDispatcher("/userAdmin/findUserById").forward(req,resp);
        }else if ("usernameSearchListFlag"==pageFlag){
            req.getRequestDispatcher("/userAdmin/findUserByUsername").forward(req,resp);
        }else if ("emailSearchListFlag"==pageFlag){
            req.getRequestDispatcher("/userAdmin/findUserByEmail").forward(req,resp);
        }else if ("bannedUserListFlag"==pageFlag){
            req.getRequestDispatcher("/userAdmin/findBannedUser").forward(req,resp);
        }
        //req.getRequestDispatcher("/admin/userAdminPageJump").forward(req,resp);
    }

    /**
     * 禁言选中的用户
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     * @author wgd
     */
    public void bannedSelectUser(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.获取所有id所对应的value
        String[] uids = req.getParameterValues("uid");
        //2.调用service方法禁言
        uas.bannedSelectedUser(uids);
        //3.跳转到userAdminPageJump - 转发
        pageFlag = req.getSession().getAttribute("pageFlag");
        if ("mixSearchListFlag"==pageFlag){
            req.getRequestDispatcher("/userAdmin/findUserByUsernameNameEmail").forward(req,resp);
        }else if ("userListFlag"==pageFlag){
            req.getRequestDispatcher("/userAdmin/userAdminPageJump").forward(req,resp);
        }else if ("idSearchListFlag"==pageFlag){
            req.getRequestDispatcher("/userAdmin/findUserById").forward(req,resp);
        }else if ("usernameSearchListFlag"==pageFlag){
            req.getRequestDispatcher("/userAdmin/findUserByUsername").forward(req,resp);
        }else if ("emailSearchListFlag"==pageFlag){
            req.getRequestDispatcher("/userAdmin/findUserByEmail").forward(req,resp);
        }else if ("bannedUserListFlag"==pageFlag){
            req.getRequestDispatcher("/userAdmin/findBannedUser").forward(req,resp);
        }
        //req.getRequestDispatcher("/admin/userAdminPageJump").forward(req,resp);

    }
    /**
     * 封号选中的用户
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     * @author wgd
     */
    public void deleteSelectUser(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.获取所有id所对应的value
        String[] uids = req.getParameterValues("uid");
        //2.调用service方法禁言
        uas.deleteSelectUser(uids);
        //3.跳转到userAdminPageJump - 转发
        pageFlag = req.getSession().getAttribute("pageFlag");
        if ("mixSearchListFlag"==pageFlag){
            req.getRequestDispatcher("/userAdmin/findUserByUsernameNameEmail").forward(req,resp);
        }else if ("userListFlag"==pageFlag){
            req.getRequestDispatcher("/userAdmin/userAdminPageJump").forward(req,resp);
        }else if ("idSearchListFlag"==pageFlag){
            req.getRequestDispatcher("/userAdmin/findUserById").forward(req,resp);
        }else if ("usernameSearchListFlag"==pageFlag){
            req.getRequestDispatcher("/userAdmin/findUserByUsername").forward(req,resp);
        }else if ("emailSearchListFlag"==pageFlag){
            req.getRequestDispatcher("/userAdmin/findUserByEmail").forward(req,resp);
        }else if ("bannedUserListFlag"==pageFlag){
            req.getRequestDispatcher("/userAdmin/findBannedUser").forward(req,resp);
        }
        //req.getRequestDispatcher("/admin/userAdminPageJump").forward(req,resp);

    }
    /**
     * 解禁选中的用户
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     * @author wgd
     */
    public void resolveSelectUser(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.获取所有id所对应的value
        String[] uids = req.getParameterValues("uid");
        //2.调用service方法禁言
        uas.resolveSelectUser(uids);
        //3.跳转到userAdminPageJump - 重定向
        pageFlag = req.getSession().getAttribute("pageFlag");
        if ("mixSearchListFlag"==pageFlag){
            req.getRequestDispatcher("/userAdmin/findUserByUsernameNameEmail").forward(req,resp);
        }else if ("userListFlag"==pageFlag){
            req.getRequestDispatcher("/userAdmin/userAdminPageJump").forward(req,resp);
        }else if ("idSearchListFlag"==pageFlag){
            req.getRequestDispatcher("/userAdmin/findUserById").forward(req,resp);
        }else if ("usernameSearchListFlag"==pageFlag){
            req.getRequestDispatcher("/userAdmin/findUserByUsername").forward(req,resp);
        }else if ("emailSearchListFlag"==pageFlag){
            req.getRequestDispatcher("/userAdmin/findUserByEmail").forward(req,resp);
        }else if ("bannedUserListFlag"==pageFlag){
            req.getRequestDispatcher("/userAdmin/findBannedUser").forward(req,resp);
        }
        //req.getRequestDispatcher("/admin/userAdminPageJump").forward(req,resp);
    }
    /**
     * 根据邮箱或者姓名或者用户名是否包含该信息查询用户
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     * @author wgd
     */
    public void findUserByUsernameNameEmail(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String userInfo = req.getParameter("userInfo");
        req.setAttribute("barid","userAdmin");
        List<UserDO> list=uas.findUserByUsernameNameEmail(userInfo);
        req.setAttribute("list",list);
        //3.跳转到userList - 重定向
        //resp.sendRedirect(req.getContextPath() + "/admin/userList");
        req.getSession().setAttribute("pageFlag","mixSearchListFlag");
        req.setAttribute("userInfo",userInfo);
        req.getRequestDispatcher("/WEB-INF/admin_view/userAdmin.jsp").forward(req, resp);
    }
    /**
     * 根据id查询用户
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     * @author wgd
     */
    public void findUserById(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String userInfo = req.getParameter("userInfo");
        req.setAttribute("barid","userAdmin");
        List<UserDO> list=uas.findUserById(userInfo);
        req.setAttribute("list",list);
        //3.跳转到userList - 重定向
        //resp.sendRedirect(req.getContextPath() + "/admin/userList");
        req.getSession().setAttribute("pageFlag","idSearchListFlag");
        req.setAttribute("userInfo",userInfo);
        req.getRequestDispatcher("/WEB-INF/admin_view/userAdmin.jsp").forward(req, resp);
    }
    /**
     * 根据id查询用户
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     * @author wgd
     */
    public void findUserByUsername(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String userInfo = req.getParameter("userInfo");
        req.setAttribute("barid","userAdmin");
        List<UserDO> list=uas.findUserByUsername(userInfo);
        req.setAttribute("list",list);
        //3.跳转到userList - 重定向
        //resp.sendRedirect(req.getContextPath() + "/admin/userList");
        req.getSession().setAttribute("pageFlag","usernameSearchListFlag");
        req.setAttribute("userInfo",userInfo);
        req.getRequestDispatcher("/WEB-INF/admin_view/userAdmin.jsp").forward(req, resp);
    }
    /**
     * 根据id查询用户
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     * @author wgd
     */
    public void findUserByEmail(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String userInfo = req.getParameter("userInfo");
        req.setAttribute("barid","userAdmin");
        List<UserDO> list=uas.findUserByEmail(userInfo);
        req.setAttribute("list",list);
        //3.跳转到userList - 重定向
        //resp.sendRedirect(req.getContextPath() + "/admin/userList");
        req.getSession().setAttribute("pageFlag","emailSearchListFlag");
        req.setAttribute("userInfo",userInfo);
        req.getRequestDispatcher("/WEB-INF/admin_view/userAdmin.jsp").forward(req, resp);
    }

    /**
     * 查询被禁言的用户
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     * @author wgd
     */
    public void findBannedUser(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setAttribute("barid","userAdmin");
        req.getSession().setAttribute("pageFlag","bannedUserListFlag");

        //2.调用service方法禁言
        List<UserDO> list=uas.findBannedUser();
        req.setAttribute("list",list);
        //3.跳转到userList - 重定向
        //resp.sendRedirect(req.getContextPath() + "/admin/userList");
        req.getRequestDispatcher("/WEB-INF/admin_view/userAdmin.jsp").forward(req, resp);
    }


}
